Skip to content

fix: ignore closed log streams during shutdown#2222

Closed
lavish0000 wants to merge 1 commit intomodelcontextprotocol:mainfrom
lavish0000:fix/closed-resource-log-race-1967
Closed

fix: ignore closed log streams during shutdown#2222
lavish0000 wants to merge 1 commit intomodelcontextprotocol:mainfrom
lavish0000:fix/closed-resource-log-race-1967

Conversation

@lavish0000
Copy link

Summary

  • guard the low-level exception logging path when the session write stream is already closed
  • add regression coverage for both ClosedResourceError and BrokenResourceError from send_log_message()

Problem

When the server receives an exception from the stream during shutdown, _handle_message() tries to emit an internal error log notification through session.send_log_message(). If the session write stream has already been torn down, that best-effort log write can raise ClosedResourceError or BrokenResourceError and crash the enclosing task group.

Fix

Treat exception logging in this shutdown path as best-effort. If the write stream is already gone, skip the log notification and continue handling the original shutdown flow.

Validation

  • uv run pytest tests/server/test_lowlevel_exception_handling.py
  • uv run ruff check src/mcp/server/lowlevel/server.py tests/server/test_lowlevel_exception_handling.py
  • uv run ruff format --check src/mcp/server/lowlevel/server.py tests/server/test_lowlevel_exception_handling.py
  • uv run pyright src/mcp/server/lowlevel/server.py tests/server/test_lowlevel_exception_handling.py

@maxisbey maxisbey added bug Something isn't working P2 Moderate issues affecting some users, edge cases, potentially valuable feature labels Mar 6, 2026
@maxisbey
Copy link
Contributor

Thanks for the PR! Per our CONTRIBUTING.md, PRs require a corresponding issue with the ready for work label. Closing for now — feel free to open an issue first and re-submit once it's triaged.

AI Disclaimer

@maxisbey maxisbey closed this Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working P2 Moderate issues affecting some users, edge cases, potentially valuable feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants